package com.ypaco.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.xml.client.Document;
import com.ypaco.ajax.client.*;
import com.google.gwt.xml.client.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class login_form implements EntryPoint {
	
	final RemoteXML rq=new RemoteXML(OSServers.ttserver()+"/inc/services/login.php","POST");
	
	public void seterror(HTML mon,String text)
	{
		mon.setHTML("<font color=\"red\">"+text+"</font>");
	}
	
	public void setSmallForm(){
		final TextBox suname=new TextBox();
		final PasswordTextBox spswd=new PasswordTextBox();
		final HTML smon=new HTML();
		final Button sbtn=new Button("Войти");
		RootPanel.get("suser").add(suname);
		RootPanel.get("spswd").add(spswd);
		RootPanel.get("smon").add(smon);
		RootPanel.get("sbtn").add(sbtn);
		sbtn.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if (suname.getText().isEmpty())
					seterror(smon,"Введите e-mail");
				else if(spswd.getValue().isEmpty())
					seterror(smon,"Введите пароль");
				else
					process(suname.getText(),spswd.getValue(),smon);
			}
		});
	}
	
	public void setBigForm(){
		final TextBox buname=new TextBox();
		final PasswordTextBox bpswd=new PasswordTextBox();
		final HTML bmon=new HTML();
		final Button bbtn=new Button("Войти");
		RootPanel.get("buser").add(buname);
		RootPanel.get("bpswd").add(bpswd);
		RootPanel.get("bmon").add(bmon);
		RootPanel.get("bbtn").add(bbtn);
		bbtn.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if (buname.getText().isEmpty())
					seterror(bmon,"Введите e-mail");
				else if(bpswd.getValue().isEmpty())
					seterror(bmon,"Введите пароль");
				else
					process(buname.getText(),bpswd.getValue(),bmon);
			}
		});
	}
	
	public void setUserForm(){
		RootPanel.get("user_logout").add(new Button("Выход",
		new ClickHandler(){
			public void onClick(ClickEvent event) {
				Location.assign(Location.getHref()+"?logout");
			}
		}));
	}
	
	public void process(String user,String pswd,HTML mon)
	{
		String data="user="+user+"&password="+pswd;
		mon.setHTML("Проверка пользователя");
		final HTML smon=mon;
		rq.request(data, new RemoteXMLRequestCallback(){

			public void onFailure(Throwable ex) {
				seterror(smon,"Ошибка связи:"+ex.getMessage());					
			}

			public void onSuccess(Document xml) {
				try
				{
					XmlProcessor proc=new XmlProcessor(xml);
					Node root=proc.getRootNode("login_result");
					Node res=proc.getNeedAttr(root, "result");
					if (res.getNodeValue().compareTo("1")==0)
					{
						res=proc.getNeedAttr(root, "usess");
						Cookies.setCookie("tt_sess", res.getNodeValue(), null, null, "/", false);
						Cookies.setCookie("tt_sess", res.getNodeValue(), null, ".somesite.ru", "/", false);
						Cookies.setCookie("tt_sess", res.getNodeValue(), null, ".othersite.ru", "/", false);
//						Cookies.setCookie("tt_sess", res.getNodeValue());
						Window.Location.reload();
						smon.setHTML("");
					}else {
						seterror(smon,"Неверное имя пользователя или пароль");
					}
				}catch(Throwable ex)
				{
					seterror(smon,"Ошибка связи:"+ex.getMessage());					
				}
				
			}
		
		});
		
	}
	
	public void onModuleLoad() {
		
		if (RootPanel.get("login_small")!=null)
			setSmallForm();
		if (RootPanel.get("big_login_form")!=null)
			setBigForm();
		if (RootPanel.get("user_logout")!=null)
			setUserForm();
	}
}
